//
//  ViewController.m
//  QiNiuDemo
//
//  Created by 杨春至 on 2017/7/11.
//  Copyright © 2017年 chzy. All rights reserved.
//

#import "ViewController.h"
#import "User.pbobjc.h"
#import <arpa/inet.h>
#import <netdb.h>
#import "Client.pbobjc.h"
#import "GameHandle.pbobjc.h"

@interface ViewController ()<GCDAsyncSocketDelegate,PomeloDelegate>
@property (nonatomic,strong) GCDAsyncSocket *socket;
@property (nonatomic,strong) Pomelo *pomeloSocket;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
#pragma mark -Pomelo Delegate-
- (void)PomeloDidConnect:(Pomelo *)pomelo{
    
}
- (void)PomeloDidDisconnect:(Pomelo *)pomelo withError:(NSError *)error{
    
}
- (void)Pomelo:(Pomelo *)pomelo didReceiveMessage:(NSArray *)message{
    
}

#pragma mark -与服务器握手
- (IBAction)connet:(id)sender {
        [self getConnet];
//    {
//        // 1.与服务器通过三次握手建立连接
//        NSString * host = @"chzbapi.bigrainbow.tv";
//        NSInteger port =  5014;
//        
//        //创建一个socket对象
//        _socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
//        
//        //连接
//        NSError *error = nil;
//        [_socket connectToHost:host onPort:port error:&error];
//        
//        if (error) {
//            NSLog(@"%@",error);
//        }
//    }
    

}
//}
- (void)getConnet{
    self.pomeloSocket = [[Pomelo alloc]initWithDelegate:self];
    NSString * host = @"chzbapi.bigrainbow.tv";
    NSInteger port =  5014;
    [self.pomeloSocket connectToHost:host onPort:port withCallback:^(id callback) {
        
    }];
}

#pragma mark -socket的代理
#pragma mark 连接成功host	__NSCFString *	@"101.37.37.129"	0x0000608000420320
-(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port{
    NSLog(@"%s",__func__);
}
#pragma mark ---发送消息测试
- (IBAction)postData:(id)sender {
    
    [self.pomeloSocket requestWithRoute:@"connector.entryHandler.entry" andParams:@{@"":@"",@"":@""} andCallback:^(id callback) {
        
    }];
//    enterWorld *client = [[enterWorld alloc]init];
//    
//    client.uid = @"asdasdasd";
//    //序列化
//    NSData *data = [client data];
//    
//    [_socket writeData:data withTimeout:0.8 tag:0];
   
}
#pragma ------
#pragma mark GCDAsyncSocket Delegate

-(void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{
    if (err) {
        NSLog(@"连接失败");
    }else{
        NSLog(@"正常断开");
    }
}

-(void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag{
    NSLog(@"%s",__func__);
    
    //发送完数据手动读取，-1不设置超时
    [sock readDataWithTimeout:-1 tag:tag];
}

-(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
    NSString *receiverStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%s %@",__func__,receiverStr);
}

#pragma mark --------
#pragma mark - C—Socket 系连接
- (void)testConnectionSocket
{
    NSString * host = @"chzbapi.bigrainbow.tv";
    NSNumber * port =  @5014;
    
    // 创建 socket
    int socketFileDescriptor = socket(AF_INET, SOCK_STREAM, 0);
    if (-1 == socketFileDescriptor) {
        NSLog(@"创建失败");
        return;
    }
    
    // 获取 IP 地址
    struct hostent * remoteHostEnt = gethostbyname([host UTF8String]);
    if (NULL == remoteHostEnt) {
        close(socketFileDescriptor);
        NSLog(@"%@",@"无法解析服务器的主机名");
        return;
    }
    
    struct in_addr * remoteInAddr = (struct in_addr *)remoteHostEnt->h_addr_list[0];
    
    // 设置 socket 参数
    struct sockaddr_in socketParameters;
    socketParameters.sin_family = AF_INET;
    socketParameters.sin_addr = *remoteInAddr;
    socketParameters.sin_port = htons([port intValue]);
    
    // 连接 socket
    int ret = connect(socketFileDescriptor, (struct sockaddr *) &socketParameters, sizeof(socketParameters));
    if (-1 == ret) {
        close(socketFileDescriptor);
        NSLog(@"连接失败");
        return;
    }
    NSLog(@"连接成功");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
